/*
 * Bomb.java
 * 
 * author: The MomberBan Team 
 * date: 15.09.2008
 */

package at.momberban.game.me;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 * @author patchinsky
 */
public class Bomb extends Sprite {
    /**
     * the bomb width
     */
    public static int BOMB_WIDTH;
    
    /**
     * the bomb height
     */
    public static int BOMB_HEIGHT;

    /**
     * define the trigger delay
     */
    public static final long TRIGGER_DELAY = 3000; // msec
    
    /**
     * get the default explosion range
     */
    public static final int DEFAULT_EXPLOSION_RANGE = 3;

    private static final String IMAGE_PATH = "/Bomb.png";

    private static final String IMAGE_PATH_EXPLOSION = "/Explosion.png";

    private static Image IMAGE_EXPLOSION;

    private static Image IMAGE_BOMB = null;

    private static Image IMAGE_UP = null;

    private static Image IMAGE_LEFT = null;

    private static Image IMAGE_RIGHT = null;

    private static Image IMAGE_DOWN = null;

    private Player player = null;
    
    private int positionId;

    private long timePlanted;

    private boolean isTriggered;
        
    private int explosionRange = DEFAULT_EXPLOSION_RANGE;

    static {
        try {
            // bomb
            IMAGE_BOMB = Image.createImage(IMAGE_PATH);
            
            BOMB_WIDTH = BOMB_HEIGHT = IMAGE_BOMB.getHeight();
            
            // explosion
            IMAGE_EXPLOSION = Image.createImage(IMAGE_PATH_EXPLOSION);
            IMAGE_RIGHT = Image.createImage(IMAGE_EXPLOSION, 0, 0, IMAGE_EXPLOSION
                    .getWidth(), IMAGE_EXPLOSION.getHeight(),
                    Sprite.TRANS_NONE);
            IMAGE_LEFT = Image.createImage(IMAGE_EXPLOSION, 0, 0, IMAGE_EXPLOSION
                    .getWidth(), IMAGE_EXPLOSION.getHeight(),
                    Sprite.TRANS_ROT180);
            IMAGE_DOWN = Image.createImage(IMAGE_EXPLOSION, 0, 0, IMAGE_EXPLOSION
                    .getWidth(), IMAGE_EXPLOSION.getHeight(),
                    Sprite.TRANS_ROT90);
            IMAGE_UP = Image.createImage(IMAGE_EXPLOSION, 0, 0, IMAGE_EXPLOSION
                    .getWidth(), IMAGE_EXPLOSION.getHeight(),
                    Sprite.TRANS_ROT270);
        } catch (IOException e) {
            Logger.log("bomb/explosion images could not be created");
            /* don't care */
        }

    }

    /**
     * constructor
     * 
     * @param image
     * @param frameWidth
     * @param frameHeight
     *             If
     */
    public Bomb() {
        super(IMAGE_BOMB, 32, 32);
        
        Media.playBombSound();
    }
    
    /**
     * let it explode
     * 
     * @return a sprite array
     * @throws IOException
     */
    public Sprite[] explode() throws IOException {
        Logger.log("bomb explode");

        Vector sprites = new Vector();
        Sprite middle = new Sprite(IMAGE_EXPLOSION,
                IMAGE_EXPLOSION.getWidth() / 3, IMAGE_EXPLOSION.getHeight());
        
        middle.setPosition(getX(), getY());
        middle.setFrame(1);
        sprites.addElement(middle);
        Logger.log("UP = " + Canvas.UP + " DOWN = " + Canvas.DOWN + " LEFT = " + Canvas.LEFT + " RIGHT = " + Canvas.RIGHT);
        for (int i = 1; i <= this.explosionRange; i++) {
            int j = Canvas.UP;
            do {
                Sprite sprite = null;
                Logger.log("Explosion-sprite: Distance to middle: " + i + "; Direction: " + j);
                if (j == Canvas.UP) {
                    sprite = new Sprite(IMAGE_UP,
                            IMAGE_EXPLOSION.getWidth() / 3, IMAGE_EXPLOSION
                                    .getHeight());
                    sprite.setPosition(getX(), getY() - sprite.getHeight() * i);
                    j = Canvas.RIGHT;
                    if (i == this.explosionRange) {
                        sprite.setFrame(0);
                    } else {
                        sprite.setFrame(2);
                    }
                } else if (j == Canvas.RIGHT) {
                    sprite = new Sprite(IMAGE_RIGHT,
                            IMAGE_EXPLOSION.getWidth() / 3, IMAGE_EXPLOSION
                                    .getHeight());
                    sprite.setPosition(getX() + sprite.getWidth() * i, getY());
                    if (i == this.explosionRange) {
                        sprite.setFrame(2);
                    } else {
                        sprite.setFrame(0);
                    }
                    j = Canvas.DOWN;
                } else if (j == Canvas.DOWN) {
                    sprite = new Sprite(IMAGE_DOWN,
                            IMAGE_EXPLOSION.getWidth() / 3, IMAGE_EXPLOSION
                                    .getHeight());
                    sprite.setPosition(getX(), getY() + sprite.getHeight() * i);
                    if (i == this.explosionRange) {
                        sprite.setFrame(2);
                    } else {
                        sprite.setFrame(0);
                    }
                    j = Canvas.LEFT;
                } else if (j == Canvas.LEFT) {
                    sprite = new Sprite(IMAGE_LEFT,
                            IMAGE_EXPLOSION.getWidth() / 3, IMAGE_EXPLOSION
                                    .getHeight());
                    sprite.setPosition(getX() - sprite.getWidth() * i, getY());
                    if (i == this.explosionRange) {
                        sprite.setFrame(0);
                    } else {
                        sprite.setFrame(2);
                    }
                    j = Canvas.UP;
                }
                sprites.addElement(sprite);
            } while (j != Canvas.UP);
        }
        Sprite[] result = new Sprite[sprites.size()];
        sprites.copyInto(result);
        return result;
    }
    
    /**
     * @param positionId to set
     */
    public void setPositionId(int positionId) {
        this.positionId = positionId;
    }
    
    /**
     * @return positionId
     */
    public int getPositionId() {
        return positionId;
    }
    
    /**
     * Getter of the property <tt>player</tt>
     * 
     * @return Returns the player.
     */

    public Player getPlayer() {
        return player;
    }

    /**
     * Setter of the property <tt>player</tt>
     * 
     * @param player
     *            The player to set.
     */
    public void setPlayer(Player player) {
        this.player = player;
    }

    /**
     * Getter of the property <tt>timePlanted</tt>
     * 
     * @return Returns the timePlanted.
     */

    public long getTimePlanted() {
        return timePlanted;
    }

    /**
     * Setter of the property <tt>timePlanted</tt>
     * 
     * @param timePlanted
     *            The timePlanted to set.
     */
    public void setTimePlanted(long timePlanted) {
        this.timePlanted = timePlanted;
    }

    /**
     * @return
     */
    public boolean isTriggered() {
        return isTriggered;
    }

    /**
     * @param isTriggered
     *            to set
     */
    public void setTriggered(boolean isTriggered) {
        Media.playExplosionSound();
        
        this.isTriggered = isTriggered;
    }
    
    /**
     * @param explosionRange to set
     */
    public void setExplosionRange(int explosionRange) {
        this.explosionRange = explosionRange;
    }
    
    /**
     * @return explosionRange
     */
    public int getExplosionRange() {
        return explosionRange;
    }
    

}
